<html>
<head><meta charset="utf-8"><title>new use for `as` · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html">new use for `as`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="195077908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195077908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Sebastian Malton <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195077908">(Apr 23 2020 at 15:27)</a>:</h4>
<p>As was mentioned in the Float to int conversions thread. Once there are more rustic versions for the all the primitive conversions that are currently covered by <code>as</code> it might make sense to remove/deprecate <code>as</code>. However, I was wondering if it would not be better to move to using <code>as</code> for calling the <code>Into</code> trait.</p>



<a name="195092083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195092083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195092083">(Apr 23 2020 at 17:12)</a>:</h4>
<p>That would be rather disruptive and break a great deal of existing code. It's theoretically possible in an edition, but I'm not sure we'd want to.</p>



<a name="195092127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195092127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195092127">(Apr 23 2020 at 17:12)</a>:</h4>
<p>It would also break people's mental parsers.</p>



<a name="195128551"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195128551" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195128551">(Apr 23 2020 at 22:44)</a>:</h4>
<p>I think that <strong>if</strong> we were going to do that, it would probably take a whole edition of warning on <code>as</code>, then another full edition of erroring on <code>as</code>.  So any potential re-use of <code>as</code> would be 7+ years away, and thus probably not worth discussing in detail at this point.</p>
<p>(That said, I fully support the library having dedicated-purpose versions of all the things that <code>as</code> can do, such as <code>wrapping_from</code> or similar.)</p>



<a name="195129461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195129461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195129461">(Apr 23 2020 at 22:55)</a>:</h4>
<p>The biggest thing i need <code>as</code> for right now is usize</p>



<a name="195130758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195130758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195130758">(Apr 23 2020 at 23:14)</a>:</h4>
<p>In indexing, or other places too?</p>



<a name="195130836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195130836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195130836">(Apr 23 2020 at 23:15)</a>:</h4>
<p>I run into issues with <code>usize</code> in three places: indexing, creating slices with <code>[0; some_calculation]</code>, and the use of <code>size_of</code>.</p>



<a name="195130886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195130886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195130886">(Apr 23 2020 at 23:16)</a>:</h4>
<p>In the former two cases I need to cast to <code>usize</code>, and in the third case I need to cast <em>from</em> <code>usize</code>.</p>



<a name="195131306"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195131306" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195131306">(Apr 23 2020 at 23:22)</a>:</h4>
<p>I was amused in <a href="https://github.com/rust-lang/rust/pull/70835#discussion_r411076438" title="https://github.com/rust-lang/rust/pull/70835#discussion_r411076438">https://github.com/rust-lang/rust/pull/70835#discussion_r411076438</a> that it was nicer to use <code>ctlz(1)</code> (the intrinsic) over <code>size_of</code> because it meant no casting</p>



<a name="195132210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195132210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195132210">(Apr 23 2020 at 23:37)</a>:</h4>
<p>Mostly indexing related stuff. <code>u32</code> / <code>usize</code> mixing is a mess, we need that portability lint system and the "let me tell cargo and rustc i'm assuming that pointers are at least 32 bits" system.</p>



<a name="195133505"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195133505" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195133505">(Apr 23 2020 at 23:54)</a>:</h4>
<p>Hmm, <code>[0; some_calculation]</code> would be even easier to have work with any length than <code>a[i]</code>, I think, since it's a const context.  </p>
<p><code>[0; u128::MAX]</code> isn't any more incorrect than <code>[0; usize::MAX]</code> already is, so too-big is fine.  Something like <code>[0; my_u8]</code> is of course fine.  And even the complaints about signed don't really apply here, since it's a const so you just get a compiler error for <code>[0; -1]</code>.  (And if anything, <code>[0; 0_i32 - 1]</code> is probably more correct than <code>[0; 0_usize - 1]</code>.)</p>



<a name="195133678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195133678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195133678">(Apr 23 2020 at 23:58)</a>:</h4>
<p>One day it'd be nice if that worked for non-const. But as long as it remains const, yes, anything seems reasonable.</p>



<a name="195133679"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195133679" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195133679">(Apr 23 2020 at 23:58)</a>:</h4>
<p>confoundingly, you can't easily make indexing impls for u32 because of how literals seem to work, you get missing trait errors when you write <code>thing[1]</code> instead of <code>thing[1_u32]</code></p>



<a name="195133770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195133770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195133770">(Apr 23 2020 at 23:59)</a>:</h4>
<p>You can just solve that by also allowing indexing by <code>i32</code>.  Which doesn't even have a speed penalty (for non-ZSTs, but I don't care about slices of ZSTs) compared to indexing by <code>u32</code> -- it's just a <code>sext</code> instead of a <code>zext</code>.</p>



<a name="195133865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195133865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195133865">(Apr 24 2020 at 00:00)</a>:</h4>
<p>But people seem to hate the thought of indexing by a signed number whenever I bring it up</p>



<a name="195137784"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195137784" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195137784">(Apr 24 2020 at 01:10)</a>:</h4>
<p>i think the fix there is to do <code>signed as unsigned</code> and then redispatch to the unsigned version, then the bounds check will catch it</p>



<a name="195137790"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195137790" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195137790">(Apr 24 2020 at 01:10)</a>:</h4>
<p>(you'd get bad panic messages i guess, but whatever)</p>



<a name="195169229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195169229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195169229">(Apr 24 2020 at 10:19)</a>:</h4>
<p>Right, that's what I mean by just needing to sign-extend, since the limit of <code>u31::MAX</code> on indexing means you only need one check.  (You could still have a different panic message that shows the index as negative, but the checks aren't harder than the unsigned case.)</p>



<a name="195231037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195231037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195231037">(Apr 24 2020 at 19:02)</a>:</h4>
<p>Wait, why would there be a limit of <code>u31::MAX</code>?</p>



<a name="195257994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195257994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195257994">(Apr 25 2020 at 00:04)</a>:</h4>
<p>sorry, that should have been <a href="https://doc.rust-lang.org/core/slice/fn.from_raw_parts.html#safety" title="https://doc.rust-lang.org/core/slice/fn.from_raw_parts.html#safety"><code>usize::MAX/2</code></a>.  You're of course correct that there's no 31-bit limit on 64-bit platforms.</p>



<a name="195277641"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195277641" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195277641">(Apr 25 2020 at 09:54)</a>:</h4>
<p><code>isize::MAX</code>, then :D</p>



<a name="195301802"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/new%20use%20for%20%60as%60/near/195301802" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/new.20use.20for.20.60as.60.html#195301802">(Apr 25 2020 at 20:35)</a>:</h4>
<p>For PNG specifically, the limit is locked at 31 bits. A PNG's dimensions are positive and not to exceed i32::MAX "because some languages have trouble with unsigned 32-bit values". That's the actual reason written into the spec.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>